﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpfTool
{
    /// <summary>
    /// Interaction logic for ComboBoxInput.xaml
    /// </summary>
    public partial class ComboBoxInput : UserControl, IPropertyGridItem
    {
        int id = 0;

        public ComboBoxInput()
        {
            InitializeComponent();

            theComboBox.SelectionChanged += new SelectionChangedEventHandler(comboboxUpdated);
        }

        public ComboBoxInput(LinkedList<string> itemList)
        {
            InitializeComponent();

            theComboBox.SelectionChanged += new SelectionChangedEventHandler(comboboxUpdated);

            addItems(itemList);
        }

        public void addItems(LinkedList<string> itemList)
        {
            foreach (string str in itemList)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = str;
                theComboBox.Items.Add(cbi);
            }
        }

        public object SelectedItemContent
        {
            get { return (theComboBox.SelectedItem as ComboBoxItem).Content; }
            set { theComboBox.SelectedItem = value; }
        }

        void comboboxUpdated(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxValueChangedEvent(this, SelectedItemContent);
        }

        public event Window1.PropertyGridItemChangeHandler ComboBoxValueChangedEvent;

        void IPropertyGridItem.registerItemUpdateEvent(wpfTool.PropertyGrid grid)
        {
            ComboBoxValueChangedEvent += new Window1.PropertyGridItemChangeHandler(grid.OnGridItemChanged);
        }

        void IPropertyGridItem.unregisterItemUpdateEvent(wpfTool.PropertyGrid grid)
        {
            ComboBoxValueChangedEvent -= grid.OnGridItemChanged;
        }

        int IPropertyGridItem.getID()
        {
            return id;
        }

        void IPropertyGridItem.setID(int i)
        {
            if (id != 0)
            {
                throw new System.Exception("ID is not 0!");
            }

            id = i;
        }
    }
}
